package com.zlx.exchange.order.controller;



import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.zlx.exchange.domain.entity.Order;
import com.zlx.exchange.order.service.OrderService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


/**
 * (Order)表控制层
 *
 * @author makejava
 * @since 2023-09-04 21:07:21
 */
@CrossOrigin
@RestController
@RequestMapping("order")
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private OrderService orderService;






    /**
     * 分页查询
     *
     * @param order   筛选条件
     * @param pageNum 查询页数
     * @param pageNum 查询总页数
     * @return 查询结果
     */

    @GetMapping("queryByAdmin")
    public ResponseEntity<?> queryByAdmin(Order order, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, Date timeBefore, Date timeAfter) {
        return this.orderService.queryByPage(order, pageNum, pageSize, timeBefore, timeAfter);
    }

    /**
     * 分页查询
     *
     * @param order   筛选条件
     * @param pageNum 查询页数
     * @param pageNum 查询总页数
     * @return 查询结果
     */
    @GetMapping("queryByUserId")
    public ResponseEntity<?> queryByUserId(Order order, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, Date timeBefore, Date timeAfter, String token) {
        JWT jwt = JWTUtil.parseToken(token);
        Integer userId = (Integer) jwt.getPayload("userId");
        if (order.getUserId().equals(userId)) {
            return this.orderService.queryByPage(order, pageNum, pageSize, timeBefore, timeAfter);
        }  else return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<?> queryById(@PathVariable("id") Long id) {
        return this.orderService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param order 实体
     * @return 新增结果
     */
    //todo 加锁

    @PostMapping("createOrder")
    public ResponseEntity<?> add(@RequestBody Order order, String token, HttpServletResponse response, HttpServletRequest request) {
        System.out.println("order"+order+"token"+token);
     return orderService.createOrder(order,token,response,request);
    }


    /**
     * 查看未付款订单
     */
    @GetMapping("queryUnpaid")
    public ResponseEntity<?> queryUnpaid(String token, Integer pageNum, Integer pageSize) {
       return orderService
               .queryUnpaid(token, pageNum, pageSize);
    }






    /**
     * 编辑数据
     *
     * @param order 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<?> edit(Order order) {
        return this.orderService.update(order);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<?> deleteById(Integer id) {
        return this.orderService.deleteById(id);
    }

}

